import os
from google.appengine.api import urlfetch
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from models import *

class MainPage(webapp.RequestHandler):
    
    def post(self):
        
        #do nothing yet
        self.response.out.write('posted')
    
    def get(self):

        # the current user       
        user = users.get_current_user()

        # render the header template
        self.response.out.write(template.render('templates/header.html', {}))
        
        # render the welcome template
        self.response.out.write(template.render('templates/welcome.html', {}))
        
        if user:
            self.response.out.write(
                '<p>Hello %s <a href="%s">Sign out</a><br>Is administrator: %s</p>' % 
                (user.nickname(), users.create_logout_url("/"), users.is_current_user_admin())
            )
            self.response.out.write('<p><a href="/shelf/list">Click here to see a list of your shelves!</a></p>')
        else:
            self.response.out.write("<p><a href=\"%s\">Sign in or register</a>.</p>" % users.create_login_url("/"))
        
        graph_values = {'shelf_count': Shelf.all().count(),
                  'item_count': ShelfItem.all().count(),
                  'meta_count': ShelfItemMeta.all().count()}
        
        self.response.out.write(template.render('templates/graphs.html', graph_values))
        
        # render the header template
        self.response.out.write(template.render('templates/footer.html', {}))

